package sockety.client.interf;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import sockety.client.add.InputScannerExt;
import sockety.client.add.OnDataReceived;

public class Client {
	
	
	private PrintWriter pw;
	private boolean isStarded = false;
	private Socket socket;

	private OnDataReceived serverDataListener;
	public void setOnDataReceived (OnDataReceived listener){this.serverDataListener = listener;}
	public OnDataReceived getOnDataReceived(){return serverDataListener;}
	
	public void start(String address, String port) {
		
		InetSocketAddress a = new InetSocketAddress(address, Integer.valueOf(port));
		socket = new Socket();
		
		try {
			socket.connect(a);
			InputScannerExt input = new InputScannerExt(socket.getInputStream(), serverDataListener);
			pw = new PrintWriter(socket.getOutputStream(), true);
			
		} catch (IOException e) {
			e.printStackTrace();
			isStarded = false;
		}
		isStarded = true;
	}
	
	public void stop (){
		try {
			socket.close();
		} catch (IOException e) {
			isStarded = true;
			e.printStackTrace();
		}
		isStarded = false;
	}
	
	public void send(String s){
		pw.println(s);
	}
	
	public boolean isStarted(){
		return isStarded;
	}
	
	

}
